<?php defined('SYSPATH') or die('No direct script access.');
/**
* Module Products Grouping (Categories, Subcategories, and Brands)
*/
class Module_Products_Productstock {
	
	private $controller;
	private $ss;
	private $thesystems;
	private $myfunctions;
	private $message;
	private $mydatetime;
	private $display;
	
	static function &factory() {
	   $o = new Module_Products_Productstock();
	  return $o;	
	}
	
	public function __construct()
	{
		$this->controller  = Request::current()->controller();
		$this->ss          = Session::instance();
		$this->thesystems  = Helper_theSystems::factory();
		$this->myfunctions = Helper_myFunctions::factory();
		$this->mydatetime  = Helper_MyDateTime::factory();
		$this->display		 = Helper_DisplayItems::factory();
		$this->message     = Language_Message::factory();
	}
	
	public function productstock($id, $command)
	{
		$rpp				= $this->thesystems->get_config('cms', 'pagination_medium_view');  // Record per page
		$halaman		= Arr::get($_GET,'page',1);
		$offset			= ($halaman - 1)*$rpp;
		$users_id		= $this->ss->get('logid');
		
		switch ($command)
		{
		case "create":
				//rubah product_id(nama) dari autocomplete jadi (id)
				$product_id		= $_POST['product_id'];
				$hapus_text_stock = explode("(",$product_id); 
				$check_products = DB::select('id','name')
					->from('products')
					->where('name','=',$hapus_text_stock['0'])
					->execute()->as_array();
				if(!empty($check_products)) {
					$products_id = $check_products[0]['id'];
				} else {
					$this->message->store_message('c_fails');
				  $r = Request::current();
				  $r->redirect($this->controller.'/productstock');
				}
				//end rubah
				
			  $datetime			= $this->mydatetime->save_to_sql_datetime_now();
			  $product_id		= $products_id;
			  $totalitems		= $_POST['totalitems'];
			  $users_id			= $users_id;
			  $description	= $_POST['description'];
			  
				$query = DB::insert('productstock', array('datetime','product_id','totalitems','users_id','description'))
				  ->values(array($datetime,$product_id,$totalitems,$users_id,$description));
			 
				$result = $query->execute();
				if ($result[1] == 1) {
					
					//add stok
					$product_stock = $this->display->show_value($product_id,'products','stock');
					$p['stock']		= $product_stock+$_POST['totalitems'];
					  
				  $query1 = DB::update('products')
					  ->set($p)
					  ->where('id', '=', $product_id)
						->execute();
				  //add stok end
				  
					$this->message->store_message('c_success');
				  $r = Request::current();
				  $r->redirect($this->controller.'/productstock');
				} else {
					$this->message->store_message('c_fails');
				}
				break;
				
		case "delete":
					// Delete Product Group Data
				  $query = DB::delete('productstock')
					  ->where('id', '=', $id)
					  ->limit(1);
				  $result = $query->execute();
					  if ($result == 1) {
					  	
					  	//kurangi stok
					  	$product_id 		= Arr::get($_REQUEST,'product_id');
							$product_stock 	= $this->display->show_value($product_id,'products','stock');
							$totalitems 		= Arr::get($_REQUEST,'totalitems');
							
							$p['stock']		= $product_stock-$totalitems;
							  
						  $query1 = DB::update('products')
							  ->set($p)
							  ->where('id', '=', $product_id)
								->execute();
						  //kurangi stok end
						  
							$this->message->store_message('d_success');
							$r = Request::current();
							$r->redirect($this->controller.'/productstock');
					  } else {
							$this->message->store_message('d_fails');
					  }
					break;
		}
		
		$count = DB::select()
			->from('productstock')
			->execute()
			->count();
		
		$productstocks = DB::select()
			->from('productstock')
			->limit($rpp)
			->offset($offset)
			->order_by('datetime', 'DESC')
			->execute()->as_array();
		
		$productstockupdate = DB::select()
			->from('productstock')
			->where('id', '=', $id)
			->limit(1)
			->execute();
		
		$pagination = Helper_myFunctions::factory()->paginations($count,$rpp);
		
		$content['title']		= 'Product Stock';
		$content['content']	= View::factory('inc/productstock')
			->set('productstocks',$productstocks)
			->set('productstockupdate',$productstockupdate[0])
			->set('pagination',$pagination)
			->render();
		
		return $content;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}